package com.kevis.zhunblog.admin.configure;

import com.kevis.webCommon.constant.UrlConstant;
import com.kevis.zhunblog.admin.interceptor.ManageAdminUserInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    RedisConnectionFactory redisConnectionFactory;

    //@Autowired
    RedisCacheConfiguration redisCacheConfiguration;

    /**
     * 接口地址忽略大小写
     *
     * @param configurer
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        AntPathMatcher matcher = new AntPathMatcher();
        matcher.setCaseSensitive(false);
        configurer.setPathMatcher(matcher);
    }

    /**
     * @return 拦截器
     * 后台拦截加入一些需要的参数各地方使用
     */
    @Bean
    public ManageAdminUserInterceptor manageAdminUserInterceptor() {
        return new ManageAdminUserInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加登录处理拦截器，拦截所有请求，登录请求除外
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(manageAdminUserInterceptor());
        //配置拦截策略
        interceptorRegistration.addPathPatterns("/**");
        //如果配置了上下文，这边不用管，底层在匹配的时候会去掉上下文
        interceptorRegistration.excludePathPatterns(UrlConstant.ADMIN_LOGIN_URL, UrlConstant.ADMIN_LOGINP_ROCESSING_URL, "/logOut", "/test/test1", "/test/test2", "/code/image");
    }

    // 设置redisTemplate实例的序列化方式，不然在用命令行查看redis时，会在前缀出现乱码
    //@Bean(name = "sstt")
//    @Bean
//    public RedisTemplate<String, Object> redisTemplate() {
//        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//
//
//        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
////        redisTemplate.setDefaultSerializer(stringSerializer);
//
//        redisTemplate.setKeySerializer(stringSerializer);
//        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
////        redisTemplate.setValueSerializer(stringSerializer);
//        redisTemplate.setHashKeySerializer(stringSerializer);
//
//        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
//        //redisTemplate.afterPropertiesSet();
//        redisTemplate.setConnectionFactory(redisConnectionFactory);
//        return redisTemplate;
//    }

//    @Bean(name = "caffeineCacheManager")
//    public CaffeineCacheManager cacheManager1() {
//        // 第一种 CaffeineCacheManager
//        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
//        cacheManager.setCaffeine(Caffeine.newBuilder()
//                .expireAfterWrite(10, TimeUnit.MINUTES)
//                .initialCapacity(100)
//                .maximumSize(10000))
//        ;
//        return cacheManager;
//    }

    //@Primary
    //@Bean(name = "redisCacheManager")
//    public RedisCacheManager cacheManager2() {
//        // 第二种 redis
//        //初始化一个RedisCacheWriter
//        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
//        //设置CacheManager的值序列化方式为json序列化
//        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
//        //RedisCacheConfiguration config =  redisCacheConfiguration;//RedisCacheConfiguration.defaultCacheConfig();
//        //序列化
//        //config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
//        //反序列化
//        //config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
//        //设置默认超过期时间是600秒
//        //config.entryTtl(Duration.ofSeconds(600));
//        //初始化RedisCacheManager
//        return new RedisCacheManager(redisCacheWriter, config);
//        //RedisCacheManager cacheManager = RedisCacheManager.create(redisConnectionFactory);
//        //return cacheManager;
//    }

//    @Bean
//    public CacheManager cacheManager(){
//        // 第一种 CaffeineCacheManager
////        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
////        cacheManager.setCaffeine(Caffeine.newBuilder()
////                .expireAfterWrite(10, TimeUnit.MINUTES)
////                .initialCapacity(100)
////                .maximumSize(10000))
////        ;
//
//        // 第二种 redis
//        //初始化一个RedisCacheWriter
//        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
//        //设置CacheManager的值序列化方式为json序列化
//        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
//        //序列化
//        config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
//        //反序列化
//        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
//        //设置默认超过期时间是600秒
//        config.entryTtl(Duration.ofSeconds(600));
//        //初始化RedisCacheManager
//        return new RedisCacheManager(redisCacheWriter, config);
//        //RedisCacheManager cacheManager = RedisCacheManager.create(redisConnectionFactory);
//        //return cacheManager;
//    }

//    @Override
//    public void addFormatters(FormatterRegistry registry) {
//        // 配置默认的日期时间格式
//        DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
//        registrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
//        registrar.setDateTimeFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//        registrar.registerFormatters(registry);
//    }
//
//    @Bean
//    public FormattingConversionService dateTimeConversionService() {
//        DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(false);
//
//        DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
//        registrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
//        registrar.setDateTimeFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//        registrar.registerFormatters(conversionService);
//
//        return conversionService;
//    }
//
//    @Bean
//    public Module dateTime() {
//        JavaTimeModule javaTimeModule = new JavaTimeModule();
//        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
//        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
//        return javaTimeModule;
//    }
//
//    @Bean
//    public ObjectMapper objectMapper() {
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
//        //objectMapper.registerModule(new LongModule());
//        objectMapper.registerModule(new SimpleModule());
//        objectMapper.registerModule(dateTime());
//        return objectMapper;
//    }
}
